package at.fhj.swd.pse.data;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import at.fhj.swd.pse.data.UserDAO;
import at.fhj.swd.pse.data.UserDAOImpl;
import at.fhj.swd.pse.JpaTestBase;
import at.fhj.swd.pse.entities.User;

public class UTUserDAO 
	extends JpaTestBase
{
	private final EntityManager m_em = getEntityManagerForPersistenceUnit("test");	
	private UserDAO m_dao;
	
	@Before
    public void setUp()
    {
        m_dao = new UserDAOImpl(m_em);        
        
        begin();
    }   

    @After
    public void tearDown()
    {
    	rollback();
    	//commit();
        closeEntityManager();
    }
    
    private User createNewUserAndInsertInDB(){
    	User user = new User();
		user.setFirstName("A");
		user.setLastName("User");
		user.setAddress("Anyroad");
		user.setCity("Nowhere");
		user.setEmail("a.user@somenet.com");
		user.setUsername("Anyone");
		user.setPassword("1234");
		
		user = m_dao.insert(user);
		return user;
    }
    
    private void assertEqualsUser(User org, User test){
		assertNotNull(test);
		assertEquals(org.getFirstName(), test.getFirstName());
		assertEquals(org.getLastName(), test.getLastName());
		assertEquals(org.getAddress(), test.getAddress());
		assertEquals(org.getCity(), test.getCity());
		assertEquals(org.getEmail(), test.getEmail());
		assertEquals(org.getUsername(), test.getUsername());
		assertEquals(org.getPassword(), test.getPassword());
    }
    
    @Test
    public void testInsertSelect()
    {
    	User user = createNewUserAndInsertInDB();
		User user_t = m_dao.get(user.getId());
		assertEqualsUser(user,user_t);
    }
    
    @Test
    public void testGetByLogin(){
    	User user = createNewUserAndInsertInDB();
    	User user_t = m_dao.getByLogin(user.getUsername(), user.getPassword());
		assertEqualsUser(user,user_t);
    }
    
    @Test
    public void testGetByLoginIllegalUsername(){
    	User user = createNewUserAndInsertInDB();
    	try{
    		m_dao.getByLogin("", user.getPassword());
        	fail();
    	}catch(IllegalArgumentException ex){
    		assertEquals("username",ex.getMessage());
    	}
    }
    
    @Test
    public void testGetByLoginUsernameNull(){
    	User user = createNewUserAndInsertInDB();
    	try{
    		m_dao.getByLogin(null, user.getPassword());
        	fail();
    	}catch(IllegalArgumentException ex){
    		assertEquals("username",ex.getMessage());
    	}
    }
    
    @Test
    public void testGetByLoginIllegalPassword(){
    	User user = createNewUserAndInsertInDB();
    	try{
    		m_dao.getByLogin(user.getUsername(), "");
        	fail();
    	}catch(IllegalArgumentException ex){
    		assertEquals("password",ex.getMessage());
    	}
    }

    @Test
    public void testGetByLoginPasswordNull(){
    	User user = createNewUserAndInsertInDB();
    	try{
    		m_dao.getByLogin(user.getUsername(), null);
        	fail();
    	}catch(IllegalArgumentException ex){
    		assertEquals("password",ex.getMessage());
    	}
    }
    
}
